﻿using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {

	public Color col;
	public Vector3 Velocity = new Vector3(0,2,0);
	public GameObject targetCollector;
	public Transform parent;
	public GameObject particlePreFab;
	// Use this for initialization
	void Start () {
		float width = Random.Range(0.5f,1.0f);
		float height = Random.Range(0.5f,1.0f);
		this.transform.parent = parent.GetChild(0).GetChild(0);
		this.transform.localScale = new Vector3(width,height,1);
		this.renderer.material.color = col;

		//Find The Target
		if(col == Color.red)
			targetCollector = parent.GetChild(0).GetChild(2).GetChild(2).gameObject;
		else if(col == Color.blue)
			targetCollector = parent.GetChild(0).GetChild(2).GetChild(0).gameObject;
		else if(col == Color.yellow)
			targetCollector = parent.GetChild(0).GetChild(2).GetChild(1).gameObject;
		else if(col == Color.green)
			targetCollector = parent.GetChild(0).GetChild(2).GetChild(3).gameObject;
	}
	void OnTriggerEnter2D(Collider2D collider)
	{
		if(collider.name == targetCollector.name)
		{
			GameObject explosion = (GameObject)Instantiate(particlePreFab,this.transform.position,this.transform.rotation);
			explosion.particleSystem.startColor = this.renderer.material.color;
			Destroy(explosion,1);
			Destroy(this.gameObject);
		}
	}
	// Update is called once per frame
	void Update () {
		transform.position += Velocity * Time.deltaTime;

		if(transform.position.y > 10)
			Destroy(this.gameObject);


	
	}
}
